// pages/o2oLocation/o2oLocation.js
var app = getApp();
// import utils from '../../../utils/city.js';
const pageSet = {
  hotel: {
    list: "plugin.hotel.frontend.hotel.hotel.get-hot-citys",
    tagList: "plugin.hotel.frontend.hotel.hotel.get-city-initials",
  },
  oil: {
    list: "plugin.easy-refuel.frontend.index.getHotCitys",
    tagList: "plugin.easy-refuel.frontend.index.getCitys",
  },
  activityHome: {
    list: "plugin.activity-apply.api.index.getHotCitys",
    tagList: "plugin.activity-apply.api.index.getCityInitials",
    tagTitle: '热门'
  },
  activitySearch: {
    list: "plugin.activity-apply.api.index.getHotCitys",
    tagList: "plugin.activity-apply.api.index.getCityInitials",
    tagTitle: '热门'
  },
  activityHome: {
    list: "plugin.activity-apply.api.index.getHotCitys",
    tagList: "plugin.activity-apply.api.index.getCityInitials",
    tagTitle: '热门'
  },
  activitySort: {
    list: "plugin.activity-apply.api.index.getHotCitys",
    tagList: "plugin.activity-apply.api.index.getCityInitials",
    tagTitle: '热门'
  },
  communityStore: {
    list: "plugin.store-community.frontend.community.getHotCitys",
    tagList: "plugin.store-community.frontend.community.getCityInitials",
  },
  communityStoreDetails: {
    list: "plugin.store-community.frontend.community.getHotCitys",
    tagList: "plugin.store-community.frontend.community.getCityInitials",
  },
  coffee: {
    list: "plugin.coffee-machine.frontend.buyer.getHotCitys",
    tagList: "plugin.coffee-machine.frontend.buyer.getCityInitials",
  },
  cityPage: {
    list: 'home-page.get-history-city-list',
    tagList: 'plugin.decorate.frotend.decorate-api.get-city',
    clickTag: 'home-page.add-history-city',
    tagTitle: '常访问'
  },
  home: {
    callbackPath: '/packageG/index/index',
    addressData: true
  },
  cake: {
    callbackPath: '/packageF/sachertorte/sachertorteIndex/sachertorteIndex',
    city: true
  },
  storeConsume: {
    id: true
  }
};

Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    tagTitle: '已开通',
    tag: '',
    sixCity: [],
    citys: [],
    city: '',
    JumpArray: [],
    position_switch:false
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    let data = {};
    if (options.tag) {
      data.tag = options.tag;
      if(pageSet[options.tag] && pageSet[options.tag].tagTitle) {
        data.tagTitle = pageSet[options.tag].tagTitle
      }
    }
    if (options.id) {
      data.id = options.id;
    }
    this.setData(data)
   
    this._getSixCityList();
    this._getCityList();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  _getSixCityList() {
    const tag = this.data.tag;
    const api = pageSet[tag]?.list || "plugin.store-cashier.frontend.store.store.getHotCitys";
    let urlStr = app.getNetAddresss(api);
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            sixCity: res.data.citys
          });
          try{
            if(res.data.position_switch == '1'){
              this.setData({
                position_switch:true
              });
            }
          }catch(err){
            console.log(err);
          }

        } else {
          console.log(res.msg);
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  _getCityList() {
    const tag = this.data.tag;
    const api = pageSet[tag]?.tagList || "plugin.store-cashier.frontend.store.store.getCityInitials";
    let urlStr = app.getNetAddresss(api);
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            citys: res.data.initials
          });
          let shortcutListData = res.data.initials.map((group) => {
            return String(group.title).substr(0, 1);
          });
          this.setData({
            shortcutList: shortcutListData
          });
          this.Jumpinit();
        } else {
          console.log(res.msg);
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  goback() {
    wx.navigateBack({
      delta: 1
    });
  },
  tosearch() {
    if(this.data.tag == 'storeConsume'){
      // 企业消费卡进来的
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc?tag=' + this.data.tag+'&id='+this.data.id
      });
      return;
    }
    if (this.data.tag) {
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc?tag=' + this.data.tag
      });
    } else {
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc'
      });
    }
  },
  setCity(e) {
    let areaname = e.currentTarget.dataset.item.full_address;
    if(this.data.tag && pageSet[this.data.tag]?.clickTag){
      this.addHistoryCity(e.currentTarget.dataset.item);
    }
    this.setData({
      city: areaname||''
    });
    let urls = '/packageC/o2o/o2oHome/o2oHome';
    if(!areaname){
      wx.setStorage({
        key: "address-point",
        data: { point: { lat: '0', lng: '0' }, city: '' }
      });
      let mapmessageData = {
        title: '',
        message: '',
        latitude: '',
        longitude: '',
        city: ''
      };
      if(this.data.tag == 'shop'){
        urls = wx.getStorageSync('diyurl');
      }
      if(this.data.tag && pageSet[this.data.tag]?.callbackPath){
        urls = pageSet[this.data.tag]?.callbackPath;
      }
      if(this.data.tag && pageSet[this.data.tag]?.addressData){
        urls = urls + '?addressData=' + JSON.stringify(mapmessageData);
      }
      wx.reLaunch({ url: urls });
    }else if (this.data.tag) {
      urls = '/packageC/o2o/o2oCity/o2oCity';
      if(this.data.tag && pageSet[this.data.tag]?.callbackPath){
        urls = pageSet[this.data.tag]?.callbackPath;
      }
      urls = urls + '?tag=' + this.data.tag + '&city=' + this.data.city;
      if(this.data.tag && pageSet[this.data.tag]?.id){
        urls = urls + '&id=' + this.data.id;
      }
      wx.navigateTo({
        url: urls
      });
    } else {
      wx.navigateTo({
        url: '/packageC/o2o/o2oCity/o2oCity?city=' + this.data.city
      });
    }
  },
  addHistoryCity(params){
    let urlStr = app.getNetAddresss(pageSet[this.data.tag].clickTag);
    app._getNetWork({ 
      url: urlStr,
      data: {
        city_id: params.id,
        city_name: params.full_address
      }
    });
  },
  Jump(e) {
    let index = e.currentTarget.dataset.index;
    // let text = e.currentTarget.dataset.text;
    wx.pageScrollTo({
      scrollTop: this.data.JumpArray[index],
      duration: 300
    });
  },
  Jumpinit() {
    let that = this;
    const query = wx.createSelectorQuery();
    query.selectAll('.d_jump').boundingClientRect();
    query.selectViewport().scrollOffset();
    query.exec(
      function(res) {
        let resData = res["0"];
        let JumpArray = [];
        JumpArray = resData.map(function(val) {
          return val.top;
        });
        that.setData({
          JumpArray: JumpArray
        });
      }
    );
  }
});
